﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Tododoo.Lib;
using Tododoo.ViewModels;
using Tododoo.Views;
using Tododoo.Data;

namespace Tododoo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private ManagedWinapi.Hotkey hotkey;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            
            RegisterHotkey();

            RegisterServices();

            Windower.Show(new ShellViewModel(), 400, 400, false);
        }

        private void RegisterServices()
        {
            var db = new DataContainer();
            Services.Register<IData>(() => db);
        }

        private void RegisterHotkey()
        {
            hotkey = new ManagedWinapi.Hotkey();
            hotkey.Alt = true;
            hotkey.KeyCode = System.Windows.Forms.Keys.Space;
            hotkey.HotkeyPressed += new EventHandler(Hotkey_HotkeyPressed);
            try
            {
                hotkey.Enabled = true;
            }
            catch (ManagedWinapi.HotkeyAlreadyInUseException)
            {
                System.Windows.MessageBox.Show("Could not register hotkey (already in use).", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void Hotkey_HotkeyPressed(object sender, EventArgs e)
        {
            Windower.Show(new AddThingViewModel(), null, null, true, true);
        }

        protected override void OnExit(ExitEventArgs e)
        {
            hotkey.Dispose();
            base.OnExit(e);
        }
    }
}
